package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.CType;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Autor' from the chart.
 * 
 * @author Lab
 * 
 */
public class Author extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_PRODUCTION = "production";
    public static final String ONT_ACTOR = "actor";
    public static final String ONT_AUTHOR = "author";
    public static final String ONT_APPELLATION = "appellation";

    public Author(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null) {
            return;
        }

        Ont.typeCheck(CType.AUTHOR_ROLE);

        final Query query = new Query();
        final String[] result = query.select("production").type("actorName", CIDOC.E82.queryValue())
                .label("actorName", this.value).type("actor", CIDOC.E39.queryValue())
                .type("production", CIDOC.E12.queryValue()).connect("actor", CIDOC.P131.queryValue(), "name")
                .connect("production", CIDOC.P14.queryValue(), "actor")
                .connect("actor", CIDOC.P2.queryValue(), CType.AUTHOR_ROLE.queryValue()).find();

        final String itemURI = Item.formatItemURI(this.id);
        String productionURI;
        if (result == null || result.length == 0) {
            productionURI = Ont.formatURI(this.id, ONT_PRODUCTION, ONT_AUTHOR);
            final String actorAppellationURI = Ont.formatURI(this.id, ONT_ACTOR, ONT_APPELLATION, ONT_AUTHOR);
            final String actorURI = Ont.formatURI(this.id, ONT_ACTOR, ONT_AUTHOR);

            Ont.getOntology().createIndividual(CIDOC.E82.getFullValue(), actorAppellationURI);
            Ont.getOntology().createIndividual(CIDOC.E39.getFullValue(), actorURI);
            Ont.getOntology().createIndividual(CIDOC.E12.getFullValue(), productionURI);

            Ont.getOntology().addLabel(actorAppellationURI, this.value);

            Ont.getOntology().connectIndividuals(actorURI, CIDOC.P131.getFullValue(), actorAppellationURI);
            Ont.getOntology().connectIndividuals(productionURI, CIDOC.P14.getFullValue(), actorURI);
            Ont.getOntology().connectIndividuals(actorURI, CIDOC.P2.getFullValue(), CType.AUTHOR_ROLE.getURI());
        } else {
            productionURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(productionURI, CIDOC.P108.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryURI = Item.formatItemQueryURI(this.id);
        final Query q = new Query();
        final String[] result = q.selectLabel().connect("production", CIDOC.P108.queryValue(), itemQueryURI)
                .type("production", CIDOC.E12.queryValue()).connect("production", CIDOC.P14.queryValue(), "actor")
                .type("actor", CIDOC.E39.queryValue())
                .connect("actor", CIDOC.P2.queryValue(), CType.AUTHOR_ROLE.queryValue())
                .connect("actor", CIDOC.P131.queryValue(), "actorName").type("actorName", CIDOC.E82.queryValue())
                .findLabel("actorName");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.AUTHOR.createField(this.value));
            doc.add(SearchMD.CONTENT.createField(this.value));
        }

    }
}
